package com.shijun.im.activity;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.TextView;

import com.shijun.im.fragment.BaseFragment;
import com.shijun.im.R;
import com.shijun.im.listener.MyFragmentListener;

/**
 * Created by zhushijun on 2017/9/23 0023.
 */

public abstract class BaseFragmentActivity extends BaseActivity implements MyFragmentListener {

    protected Button backButton, rightButton;//标题栏左边按钮和右边按钮
    protected TextView titleTextView;//标题栏的标题
    protected int resid;//Fragment要显示的区域view的id
    private OnFinishListener listener;

    protected abstract void initView();

    /**
     * 初始化首个Fragment
     */
    protected void initFragment(BaseFragment fragment) {
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        fragmentTransaction.replace(getFragmentLayout(), fragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }

    /**
     * 监听按键
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        super.onKeyDown(keyCode, event);
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (getFragmentManager().getBackStackEntryCount() == 1) {
                finish();
                return true;
            }
            closeSoftKeyboard();
        }
        return true;
    }

    public Button getBackButton() {
        return backButton;
    }

    public Button getRightButton() {
        return rightButton;
    }

    public TextView getTitleTextView() {
        return titleTextView;
    }

    public void setFragmentLayout(int resid) {
        this.resid = resid;
    }

    public int getFragmentLayout() {
        return resid;
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public void turnToFragment(Fragment fragment) {
        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
        fragmentTransaction.replace(resid, fragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }

    @Override
    public void turnToFragment(Fragment fragment, String name) {
        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
        fragmentTransaction.replace(resid, fragment);
        fragmentTransaction.addToBackStack(name);
        fragmentTransaction.commit();
    }

    public void setOnFinishListener(OnFinishListener listener) {
        this.listener = listener;
    }

    public interface OnFinishListener {
        void finish();
    }

    @Override
    protected int setStatusBarColor() {
        return R.color.colorWhite;
    }

    @Override
    protected boolean isSettingStatusBarText() {
        return true;
    }

    @Override
    protected int loadLayout() {
        return R.layout.activity_base_moneybag;
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        backButton = (Button) findViewById(R.id.activity_base_fragment_title_left_but);
        rightButton = (Button) findViewById(R.id.activity_base_fragment_title_right_but);
        titleTextView = (TextView) findViewById(R.id.activity_base_fragment_title_tag_tx);

        initView();
    }

    private void closeSoftKeyboard() {
        View view = this.getWindow().peekDecorView();
        if (view != null) {
            InputMethodManager inputMethodManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }
}
